We've mentioned it
briefly before, but let's dive deeper into autoscaling and
on-demand containers. Let's discuss what it is, what it means, and
why it is useful. In the hands-on lab
you just completed, you learned that
Cloud Run starts your container to handle
incoming requests. Multiple requests can be
handled by a container at the same time concurrently
and Cloud Run will not shut down a
container that it is handling requests unless something bad happens like an
application crash. Cloud Run automatically
increases capacity when necessary to make sure it
handles all incoming requests. This feature is known as autoscaling and this
is how it works. Every service has an
internal load balancer, the load balancer distributes requests over the group
of available containers. If all containers are busy, Cloud Run adds additional
containers to the service. As soon as a demand
decreases again, Cloud Runs stop sending traffic to some containers
and shuts them down. The number of
container instances in a Cloud Run service is limited to 1000 instances by default. If you need more, you can submit a request for
a quota increase. However, over 1000 containers can unleash a lot
of concurrency on downstream systems and APIs and it might be more
than they can handle. You'll find yourself
lowering the limit more often to increase the
stability of your system. When no requests come to
the service for awhile, Cloud Run shuts down
all containers. A fresh container will start on-demand as soon as a
new request comes in. You might also know
this as scale to zero. This process is attractive for economic reasons
because you're not paying for idling containers. The pricing mode along
Cloud Run is unique because you pay only for the
system resources you use while a container is
handling a request with a granularity of
a 100 milliseconds. Most of the other
compute products, such as Compute Engine, charge for servers
that are running, even if you're not using them. That means you're often paying
for idle server capacity. We should emphasize
that you are charge for container time and
not request time. If your container can handle 50 requests at the same time, you're paying one time and not 50 times for your container. The price of container time increases with CPU and memory. A container with more vCPU
and memory is more expensive. Now that you know how
the pricing model of Cloud Run works, it won't be a surprise that the lifetime of a container on Cloud Run is only guaranteed while the container
is handling requests. If a container is not
handling any requests, Cloud Run can shut it down. This doesn't mean it
will but it might. In fact, containers
live much longer than you would expect if are
not handling requests. You may wonder if you
can get free CPU time if work is being done
while a container is idle, well, the short answer is no. As soon as a
container is handling no requests the CPU is
throttled to nearly zero, this means that your application will run at a really slow pace. Because Cloud Run shuts down and throttles idle containers, you should complete
all work before you return the HTTP request. This means that any tasks you
run in the background after returning an HTTP request
might not finish. To be absolutely
clear, we're not saying that you shouldn't
use background threads. Just make sure they finish processing before you return
the request to the user. That request might be
the last request to run on that container
before it quits. The solution is to turn your background tasks
into HTTP requests. Two products on Google
Cloud help with this; Cloud Tasks and Cloud Scheduler. They're both
well-integrated with Cloud Run and we'll look
at them in a later module. On a traditional server, you can store data directly on the server instance
or on a shared disk, on Cloud Run, that's
not possible. Although the disposable
containers on Cloud Run have a small
disk you can write to, the entire file system is in memory and disappears when
the container is shut down. This means you'll need to store persistent data in a
downstream system. For example, you can store
binary data in Cloud Storage, session data in Redis, that's memory store
on Google Cloud and your application data in
a relational database. The key thing to remember is that while you can
write data to disk, you should persist your data downstream if you
want to keep it. There are a lot of options
to serve requests your way. Most applications handle
plain HTTP / 1 requests, like many apps do. If you need WebSockets for interactive applications,
that works as well. If you want to use HTTP / 2 the more modern of
performance version of HTTP, Cloud Run forwards
the connection from clients all the way through application and you
can use streaming responses. With the streaming protocols, there are no limits on
request and response size. If your client support HTTP / 2 and your
application doesn't, Cloud Run builds an HTTP
/ 2 connection with the client and downgrades the protocol to communicate
with your application. This delivers some of
the performance benefits of HTTP / 2, while you don't have
to change anything.